package com.kcs.fxmargin.service;

import java.sql.Connection;
import java.sql.SQLException;

import org.apache.log4j.Logger;

import com.kcs.common.utils.ConnectionUtils;
import com.kcs.common.utils.Constant;
import com.kcs.fxmargin.dao.TRateHeaderDAO;
import com.kcs.fxmargin.model.TRateHeader;

public class TRateHeaderService {
	private Logger log = Logger.getLogger(Constant.LOG_FX);
	
	public int insert(TRateHeader tRateList,Connection conn) throws Exception{
		int res = -1;
		try{
			TRateHeaderDAO dao = new TRateHeaderDAO();
			 res = dao.insert(tRateList, conn);

			
		} catch (Exception e){
			log.error(e);
			e.printStackTrace();
			throw e;
		} 
		return res;
	}
	
	
	
	public int updateByCondition(TRateHeader model,Connection conn) throws Exception{
		int res = -1;
		try{
			TRateHeaderDAO dao = new TRateHeaderDAO();
			res = dao.updateByCondition(model, conn);	
		} catch (Exception e){
			log.error(e);
			e.printStackTrace();
			throw e;
		} 
		return res;
	}
	
	public int deleteRateHeader(Connection conn, String id) throws Exception{
		int res = -1;
		try{
			TRateHeaderDAO dao = new TRateHeaderDAO();
			res = dao.deleteRateHeader(conn, id);
		} catch (Exception e){
			log.error(e);
			e.printStackTrace();
			throw e;
		} 
		return res;
	}

	public boolean isExistHeader(TRateHeader model,Connection conn) throws Exception{
		boolean isExist = false;

		try{
			TRateHeaderDAO dao = new TRateHeaderDAO();
			
			isExist = dao.isExistHeader(model, conn);

		
		} catch (Exception e){
			log.error(e);
			e.printStackTrace();
			throw e;
		} 
		return isExist;
	}
	
	public boolean isOlderAsOfDateAndTime(TRateHeader model,Connection conn) throws Exception{
		boolean res = false;
		try{
			TRateHeaderDAO dao = new TRateHeaderDAO();
			res = dao.isOlderAsOfDateAndTime(model, conn);
		
		} catch (Exception e){
			log.error(e);
			e.printStackTrace();
			throw e;
		} 		
		return res;
	}
	
	public TRateHeader getCurrentActiveHeader(TRateHeader model,Connection conn) throws Exception{
		TRateHeader header = null;

		try{
			TRateHeaderDAO dao = new TRateHeaderDAO();
			
			header = dao.getCurrentActiveHeader(model, conn);

		
		} catch (Exception e){
			log.error(e);
			e.printStackTrace();
			throw e;
		} 
		return header;
	}
	
	public void callProc(){
		Connection conn = null;//
		try{
			  conn = ConnectionUtils.getConnection(Constant.MSSQL, Constant.TO);
			//for(int i=0;i<10;i++){
				log.debug("callSPRevaluationRate");
				TRateHeaderDAO dao = new TRateHeaderDAO();				
				dao.callSPRevaluationRate(conn);
			//}
		} catch (Exception e) {
			e.printStackTrace();
		} finally {
			if (conn!=null ){
				try {
					conn.close();
				} catch (SQLException e) {
					// TODO Auto-generated catch block
					log.error(e);
					e.printStackTrace();
				}		
			}
		}
	}
}
